/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TelaLogin.java
 *
 * Created on 05/04/2011, 13:43:42
 */
package Tela;

import Classe.BD;
import Classe.Funcionario;
import Classe.Login;
import java.awt.Component;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import menu.Menu;

/**
 *
 * @author gladson
 */
public class TelaLogin extends javax.swing.JFrame {

    private Component frameI;

    /** Creates new form TelaLogin */
    public TelaLogin() {
        initComponents();
        this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jpnFundoLogin = new javax.swing.JPanel();
        jpnLoginInterno = new javax.swing.JPanel();
        lblUsuarioLogin = new javax.swing.JLabel();
        sTxtUsuarioLogin = new javax.swing.JTextField();
        sTxtSenhaLogin = new javax.swing.JPasswordField();
        lblSenhaLogin = new javax.swing.JLabel();
        btEntrarLogin = new javax.swing.JButton();
        btSairLogin = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        jpnFundoLogin.setBackground(new java.awt.Color(204, 0, 51));
        jpnFundoLogin.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED), javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED)));
        jpnFundoLogin.setPreferredSize(new java.awt.Dimension(800, 600));

        jpnLoginInterno.setBackground(new java.awt.Color(255, 255, 255));
        jpnLoginInterno.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED), javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED)));

        lblUsuarioLogin.setFont(new java.awt.Font("Arial", 1, 14));
        lblUsuarioLogin.setText("Usúario");

        sTxtUsuarioLogin.setFont(new java.awt.Font("Arial", 0, 12));
        sTxtUsuarioLogin.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)), javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED)));
        sTxtUsuarioLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sTxtUsuarioLoginActionPerformed(evt);
            }
        });

        sTxtSenhaLogin.setFont(new java.awt.Font("Arial", 0, 12));
        sTxtSenhaLogin.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)), javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED)));
        sTxtSenhaLogin.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                sTxtSenhaLoginKeyTyped(evt);
            }
        });

        lblSenhaLogin.setFont(new java.awt.Font("Arial", 1, 14));
        lblSenhaLogin.setText("Senha");

        btEntrarLogin.setFont(new java.awt.Font("Arial", 1, 14));
        btEntrarLogin.setText("Entrar");
        btEntrarLogin.setToolTipText("Entrar");
        btEntrarLogin.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED), javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED)));
        btEntrarLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        btSairLogin.setFont(new java.awt.Font("Arial", 1, 14));
        btSairLogin.setText("Sair");
        btSairLogin.setToolTipText("Sair");
        btSairLogin.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED), javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED)));
        btSairLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IMG/Sem título.jpg"))); // NOI18N

        jLabel12.setFont(new java.awt.Font("Magneto", 3, 58));
        jLabel12.setForeground(new java.awt.Color(255, 153, 153));
        jLabel12.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel12.setText("Oficina Gurgel");

        javax.swing.GroupLayout jpnLoginInternoLayout = new javax.swing.GroupLayout(jpnLoginInterno);
        jpnLoginInterno.setLayout(jpnLoginInternoLayout);
        jpnLoginInternoLayout.setHorizontalGroup(
            jpnLoginInternoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpnLoginInternoLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(6, 6, 6)
                .addGroup(jpnLoginInternoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel12, javax.swing.GroupLayout.PREFERRED_SIZE, 493, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jpnLoginInternoLayout.createSequentialGroup()
                        .addGap(37, 37, 37)
                        .addGroup(jpnLoginInternoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblUsuarioLogin)
                            .addComponent(lblSenhaLogin))
                        .addGap(18, 18, 18)
                        .addGroup(jpnLoginInternoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(jpnLoginInternoLayout.createSequentialGroup()
                                .addComponent(btEntrarLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(btSairLogin))
                            .addComponent(sTxtUsuarioLogin, javax.swing.GroupLayout.DEFAULT_SIZE, 251, Short.MAX_VALUE)
                            .addComponent(sTxtSenhaLogin))))
                .addContainerGap(101, Short.MAX_VALUE))
        );

        jpnLoginInternoLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btEntrarLogin, btSairLogin});

        jpnLoginInternoLayout.setVerticalGroup(
            jpnLoginInternoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpnLoginInternoLayout.createSequentialGroup()
                .addGroup(jpnLoginInternoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jpnLoginInternoLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel12, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(jpnLoginInternoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(sTxtUsuarioLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblUsuarioLogin))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jpnLoginInternoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(sTxtSenhaLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblSenhaLogin))
                        .addGap(18, 18, 18)
                        .addGroup(jpnLoginInternoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btEntrarLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btSairLogin)))
                    .addGroup(jpnLoginInternoLayout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addComponent(jLabel1)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jpnLoginInternoLayout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {sTxtSenhaLogin, sTxtUsuarioLogin});

        jpnLoginInternoLayout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {btEntrarLogin, btSairLogin});

        javax.swing.GroupLayout jpnFundoLoginLayout = new javax.swing.GroupLayout(jpnFundoLogin);
        jpnFundoLogin.setLayout(jpnFundoLoginLayout);
        jpnFundoLoginLayout.setHorizontalGroup(
            jpnFundoLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpnFundoLoginLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jpnLoginInterno, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jpnFundoLoginLayout.setVerticalGroup(
            jpnFundoLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpnFundoLoginLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jpnLoginInterno, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jpnFundoLogin, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jpnFundoLogin, javax.swing.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE)
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-808)/2, (screenSize.height-276)/2, 808, 276);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        this.dispose();
        // TODO add your handling code here:
}//GEN-LAST:event_jButton2ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        if (validacao()) {

            Login login = new Login();
            login.setLogin(sTxtUsuarioLogin.getText().trim().toUpperCase());
            login.setSenha(new String(sTxtSenhaLogin.getPassword()).toUpperCase().trim());

            String sql = "SELECT * FROM LOGIN L INNER JOIN FUNCIONARIO F "
                    + "ON L.ID_FUNCIONARIO = F.ID_FUNCIONARIO "
                    + "WHERE L.LOGIN='" + login.getLogin() + "' AND L.SENHA='" + login.getSenha() + "'";
            
            BD banco = new BD();
            try {
                ResultSet rs = banco.consultar(sql);
                if (rs.next()) {
                    Funcionario fun = new Funcionario();
                    fun.setIdFuncionario(rs.getInt("ID_FUNCIONARIO"));
                    fun.setNomePessoa(rs.getString("NOME_FUNCIONARIO"));
                    Menu menu = new Menu();
                    menu.inicio(fun);
                    menu.setVisible(true);
                    this.dispose();

                } else {
                    JOptionPane.showMessageDialog(frameI, "Usuário e/ou senha inválido(s)");
                }

            } catch (ClassNotFoundException ex) {
                JOptionPane.showMessageDialog(frameI, "Banco de Dados Indisponível");
                Logger.getLogger(TelaLogin.class.getName()).log(Level.SEVERE, null, ex);

            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(frameI, "Erro encontrado ao buscar usuário");
                Logger.getLogger(TelaLogin.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

}//GEN-LAST:event_jButton4ActionPerformed

    private void sTxtUsuarioLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sTxtUsuarioLoginActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_sTxtUsuarioLoginActionPerformed

    private void sTxtSenhaLoginKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_sTxtSenhaLoginKeyTyped
       int i = evt.getKeyChar();
    if (i==10){//The ENTER KEY
        if (validacao()) {

            Login login = new Login();
            login.setLogin(sTxtUsuarioLogin.getText().trim().toUpperCase());
            login.setSenha(new String(sTxtSenhaLogin.getPassword()).toUpperCase().trim());

            String sql = "SELECT * FROM LOGIN L INNER JOIN FUNCIONARIO F "
                    + "ON L.ID_FUNCIONARIO = F.ID_FUNCIONARIO "
                    + "WHERE L.LOGIN='" + login.getLogin() + "' AND L.SENHA='" + login.getSenha() + "'";
            
            BD banco = new BD();
            try {
                ResultSet rs = banco.consultar(sql);
                if (rs.next()) {
                    Funcionario fun = new Funcionario();
                    fun.setIdFuncionario(rs.getInt("ID_FUNCIONARIO"));
                    fun.setNomePessoa(rs.getString("NOME_FUNCIONARIO"));
                    Menu menu = new Menu();
                    menu.inicio(fun);
                    menu.setVisible(true);
                    this.dispose();

                } else {
                    JOptionPane.showMessageDialog(frameI, "Usuário e/ou senha inválido(s)");
                }

            } catch (ClassNotFoundException ex) {
                JOptionPane.showMessageDialog(frameI, "Banco de Dados Indisponível");
                Logger.getLogger(TelaLogin.class.getName()).log(Level.SEVERE, null, ex);

            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(frameI, "Erro encontrado ao buscar usuário");
                Logger.getLogger(TelaLogin.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
       
    }
    }//GEN-LAST:event_sTxtSenhaLoginKeyTyped

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new TelaLogin().setVisible(true);
            }
        });




    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btEntrarLogin;
    private javax.swing.JButton btSairLogin;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JPanel jpnFundoLogin;
    private javax.swing.JPanel jpnLoginInterno;
    private javax.swing.JLabel lblSenhaLogin;
    private javax.swing.JLabel lblUsuarioLogin;
    private javax.swing.JPasswordField sTxtSenhaLogin;
    private javax.swing.JTextField sTxtUsuarioLogin;
    // End of variables declaration//GEN-END:variables

    public boolean validacao() {
        boolean retorno = true;

        if (sTxtUsuarioLogin.getText().trim().length() == 0) {
            retorno = false;
            JOptionPane.showMessageDialog(frameI, "O preenchimento do campo Usuário é obrigatório!");
        }
        if (sTxtSenhaLogin.getPassword().toString().trim().length() == 0) {
            retorno = false;
            JOptionPane.showMessageDialog(frameI, "O preenchimento do campo Senha é obrigatório!");
        }

        return retorno;
    }
}
